//
//  RBDatePickerVC.m
//  RBDemoProj
//
//  Created by Lan on 2019/1/3.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBDatePickerVC.h"
#import "RBDefine.h"
#import "UIViewController+RBAdd.h"
#import <UIView+YYAdd.h>

@interface RBDatePickerVC ()

@property(nonatomic, copy) void (^block)(NSDate *date);

@end

@implementation RBDatePickerVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.contentView.width = kRBMainScreenWidth;
    self.contentView.height = self.contentView.height;
    self.contentView.left = 0;
    self.contentView.top = kRBMainScreenHeight;
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
}

#pragma mark - Public

- (void)showWithMaxDate:(NSDate *)maxDate
                minDate:(NSDate *)minDate
            currentDate:(NSDate *)currentDate
                 fromVc: (UIViewController *)fromVc
         datePickerMode: (UIDatePickerMode)mode
             completion:(void (^)(NSDate *))block
{
    self.block = block;
    [self.view addSubview:self.contentView];
    [fromVc presentVc:self];
    self.datePicker.datePickerMode = mode;
    self.datePicker.maximumDate = maxDate;
    self.datePicker.minimumDate = minDate;
    self.datePicker.date = currentDate;
}

- (void)show
{
    [UIView animateWithDuration:0.05 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.contentView.bottom = kRBMainScreenHeight - kRBSafeBottomHeight;
        }];
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.15 animations:^{
        self.contentView.top = kRBMainScreenHeight;
    }];
    [UIView animateWithDuration:0.05 delay:0.1 options:0 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

#pragma mark - Event

- (IBAction)cancelBtnTapped:(id)sender
{
    [self dismiss];
}

- (IBAction)confirmBtnTapped:(id)sender
{
    if (self.block)
    {
        self.block(self.datePicker.date);
    }
    [self dismiss];
}

#pragma mark - Getter

@end
